package com.hy.frame.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.hy.frame.common.IAppUI;
import com.hy.frame.common.IApplication;
import com.hy.frame.common.IBaseFragment;
import com.hy.frame.common.IBaseUI;
import com.hy.frame.common.ILifeUI;

/**
 * title BaseFragment
 * author HeYan
 * time 2020/12/2 16:50
 * desc 无
 */
public abstract class BaseFragment extends SuperFragment implements IBaseUI, ILifeUI, IAppUI, IBaseFragment<SuperFragment> {

    private IApplication mApp = null;
    private Bundle mArgs = null;

    private boolean mDestroy;
    private boolean mPause;
    private boolean mStop;
    private boolean mResume;

    @Override
    public IApplication getCurApp() {
        if (this.mApp == null && getCurActivity() != null && getCurActivity().getApplication() instanceof IApplication) {
            this.mApp = (IApplication) getCurActivity().getApplication();
        }
        return this.mApp;
    }

    @Override
    public Activity getCurActivity() {
        return getActivity();
    }

    @Override
    public Bundle getArgs() {
        if (this.mArgs == null) {
            this.mArgs = getArguments();
        }
        return this.mArgs;
    }

    public String getStrings(int... ids) {
        StringBuilder sb = new StringBuilder();
        for (int id : ids) {
            sb.append(getString(id));
        }
        return sb.toString();
    }

    @Override
    public void startAct(Class<?> cls) {
        startAct(cls, null, null);
    }

    @Override
    public void startAct(Class<?> cls, Bundle bundle, Intent intent) {
        startActForResult(cls, 0, bundle, intent);
    }

    @Override
    public void startActForResult(Class<?> cls, int requestCode) {
        startActForResult(cls, requestCode, null, null);
    }

    @Override
    public void startActForResult(Class<?> cls, int requestCode, Bundle bundle, Intent intent) {
        if (getCurContext() == null) return; //判断Context是否为空
        Intent i = intent;
        if (i == null)
            i = new Intent();
        Bundle b = bundle;
        if (b == null)
            b = new Bundle();
        b.putString(IAppUI.ARG_LAST_ACT, getClass().getSimpleName());
        i.putExtra(IAppUI.ARG_BUNDLE, b);
        i.setClass(getCurContext(), cls);
        if (requestCode != 0)
            startActivityForResult(i, requestCode);
        else
            startActivity(i);
    }

    @Override
    public Context getCurContext() {
        return getContext();
    }

//    @Override
//    public void initView() {
//
//    }

//    @Override
//    public void initData() {
//
//    }
//
//    @Override
//    public void onViewClick(View v) {
//
//    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (!initAttrs()) return null;
        return getRootLayout(inflater, container, savedInstanceState);
    }

    /**
     * 获取根布局
     */
    abstract View getRootLayout(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState);

    @Override
    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        initView();
        initData();
    }

    /**
     * 初始化基本属性
     *
     * @return boolean
     */
    private boolean initAttrs() {
        Activity activity = getCurActivity();
        return activity != null;
    }

    @Override
    public void onResume() {
        super.onResume();
        this.mPause = false;
        this.mStop = false;
        this.mResume = true;
    }

    @Override
    public void onPause() {
        super.onPause();
        this.mPause = true;
        this.mResume = false;
    }

    @Override
    public void onStop() {
        super.onStop();
        this.mStop = true;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mDestroy = true;
    }

    /**
     * 是否已经销毁
     */
    @Override
    public boolean isIDestroy() {
        return mDestroy;
    }

    /**
     * 是否已经暂停
     */
    @Override
    public boolean isIPause() {
        return mPause;
    }

    /**
     * 是否已经停止
     */
    @Override
    public boolean isIStop() {
        return mStop;
    }

    /**
     * 是否已经恢复
     */
    @Override
    public boolean isIResume() {
        return mResume;
    }
}
